package parser

import (
	"fmt"
	"reflect"
	"strings"
)

type ASTNode struct{}

func (node *ASTNode) String() string {
	var fields []string
	// 使用反射遍历结构体字段
	t := reflect.TypeOf(node)  // 获取类型信息
	v := reflect.ValueOf(node) // 获取值信息

	// 遍历结构体的字段
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i) // 获取字段信息
		value := v.Field(i) // 获取字段值
		fmt.Printf("Field Name: %s, Field Type: %s, Field Value: %v\n", field.Name, field.Type, value)
		fields = append(fields, fmt.Sprintf("%s=%v", field.Name, value))
	}
	fieldsStr := strings.Join(fields, " ")
	return fmt.Sprintf("<%s>%s", t.Name(), fieldsStr)
}

type Select struct {
	ASTNode
	Targets   []string
	FromTable string
	Where     string
	GroupBy   string
	OrderBy   *OrderBy
}

type OrderBy struct {
	ASTNode
	Column    string
	Direction Direction
}

type Direction string

const (
	ASC  Direction = "ASC"
	DESC Direction = "DESC"
)

type JoinType string

const (
	LEFT_JOIN  JoinType = "LEFT JOIN"
	RIGHT_JOIN JoinType = "RIGHT JOIN"
	INNER_JOIN JoinType = "INNER JOIN"
	CROSS_JOIN JoinType = "CROSS JOIN"
	FULL_JOIN  JoinType = "FULL JOIN"
)

type Join struct {
	ASTNode
	Left      ASTNode
	Right     ASTNode
	JoinType  JoinType
	Condition string
}

type Identifier struct {
	ASTNode
	Parts []string
}

type Star struct {
	ASTNode
}

type Constant struct {
	ASTNode
	Value any
}

type Operation struct {
	ASTNode
	Op   string
	Args []ASTNode
}

type BinaryOperation struct {
	Operation
}

type FunctionOperation struct {
	Operation
}

type Update struct {
	ASTNode
	Table   string
	Columns []string
	Where   string
}

type Insert struct {
	ASTNode
	Table   string
	Columns []string
	Values  [][]any
}

type Delete struct {
	ASTNode
	Table string
	Where string
}

type CreateTable struct {
	ASTNode
	Table       string
	Columns     []string
	PrimaryKeys []string
}

type DropTable struct {
	ASTNode
	Table string
}

type CreateIndex struct {
	ASTNode
	Table  string
	Column string
	Unique bool
}

type DropIndex struct {
	ASTNode
	Table  string
	Column string
}

type Command struct {
	ASTNode
	Commands []ASTNode
}

type Explain struct {
	ASTNode
	Statement ASTNode
}
